package com.skyline.spot.dao.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.base.type.SpotRefrenceType;
import com.skyline.common.bean.Page;
import com.skyline.spot.dao.SpotRefrenceDao;
import com.skyline.spot.mapper.SpotRefrenceMapper;
import com.skyline.spot.model.SpotRefrence;

@Repository("spotRefrenceDao")
public class SpotRefrenceDaoImpl extends BaseDaoImpl implements SpotRefrenceDao {

	/**
	 * insert
	 */
	@Value("${SpotRefrenceDao.insertRefrence}")
	private String insertRefrenceSQL;

	/**
	 * query
	 */
	@Value("${SpotRefrenceDao.queryRefrencesBySpotId}")
	private String queryRefrencesBySpotIdSQL;

	@Value("${SpotRefrenceDao.queryRefrencesBySpotIdAndType}")
	private String queryRefrencesBySpotIdAndTypeSQL;

	@Value("${SpotRefrenceDao.queryRefrencesByRefrenceIdAndSpotId}")
	private String queryRefrencesByRefrenceIdAndSpotIdSQL;

	@Value("${SpotRefrenceDao.queryRefrencesByType}")
	private String queryRefrencesByTypeSQL;

	@Value("${SpotRefrenceDao.queryRefrencesBySpotNameAndType}")
	private String queryRefrencesBySpotNameAndTypeSQL;

	@Value("${SpotRefrenceDao.queryRefrencesByProviderIdAndSpotIdAndType}")
	private String queryRefrencesByProviderIdAndSpotIdAndTypeSQL;

	@Value("${SpotRefrenceDao.queryRefrencesByProviderIdAndSpotId}")
	private String queryRefrencesByProviderIdAndSpotIdSQL;

	@Value("${SpotRefrenceDao.getRefrenceCountBySpotIdAndType}")
	private String getRefrenceCountBySpotIdAndTypeSQL;

	@Value("${SpotRefrenceDao.getRefrenceCountBySpotIdAndProviderIdAndType}")
	private String getRefrenceCountBySpotIdAndProviderIdAndTypeSQL;

	@Value("${SpotRefrenceDao.getRefrenceCountBySpotIdAndProviderId}")
	private String getRefrenceCountBySpotIdAndProviderIdSQL;

	@Value("${SpotRefrenceDao.queryRefrenceById}")
	private String queryRefrenceByIdSQL;

	/**
	 * delete
	 */
	@Value("${SpotRefrenceDao.deleteRefrenceById}")
	private String deleteRefrenceByIdSQL;

	@Value("${SpotRefrenceDao.deleteRefrenceByRefrenceIdAndSpotId}")
	private String deleteRefrenceByRefrenceIdAndSpotIdSQL;

	@Value("${SpotRefrenceDao.deleteRefrenceByRefrenceIdAndSpotIdAndType}")
	private String deleteRefrenceByRefrenceIdAndSpotIdAndTypeSQL;

	@Value("${SpotRefrenceDao.deleteRefrenceByProviderIdAndSpotId}")
	private String deleteRefrenceByProviderIdAndSpotIdSQL;

	@Value("${SpotRefrenceDao.deleteRefrenceByProviderIdAndSpotIdAndType}")
	private String deleteRefrenceByProviderIdAndSpotIdAndTypeSQL;

	@Override
	public Long insertRefrence(Long spotId, String spotName,
			String spotPortrait, Long refrenceId,
			String refrencePortrait, String refrenceTitle,
			String refrenceDigest, SpotRefrenceType type,
			Long refrenceProviderId, String refrenceProviderNickname,
			String refrenceProviderPortrait, String refrenceProviderEmail) {

		return this.insertWithIdReturn(insertRefrenceSQL, spotId, spotName,spotPortrait,
				refrenceId, refrencePortrait, refrenceTitle, refrenceDigest,
				type.toString(), refrenceProviderId, refrenceProviderNickname,
				refrenceProviderPortrait, refrenceProviderEmail);

	}

	/**************************** query **************************/
	@Override
	public List<SpotRefrence> queryRefrencesBySpotId(Long spotId,
			Activity activity, Page page) {
		// TODO Auto-generated method stub
		if (page == null) {
			List<SpotRefrence> list = jdbcTemplate.query(
					queryRefrencesBySpotIdSQL, SpotRefrenceMapper.getMapper(),
					spotId, activity.toString());
			return list;
		} else {
			List<SpotRefrence> list = this
					.getPaginationResult(queryRefrencesBySpotIdSQL, page,
							SpotRefrenceMapper.getMapper(), spotId,
							activity.toString());
			return list;

		}
	}

	@Override
	public List<SpotRefrence> queryRefrencesBySpotIdAndType(Long spotId,
			SpotRefrenceType type, Activity activity, Page page) {
		// TODO Auto-generated method stub
		if (page == null) {
			List<SpotRefrence> list = jdbcTemplate.query(
					queryRefrencesBySpotIdAndTypeSQL,
					SpotRefrenceMapper.getMapper(), spotId, type.toString(),
					activity.toString());
			return list;
		} else {
			return this.getPaginationResult(queryRefrencesBySpotIdAndTypeSQL,
					page, SpotRefrenceMapper.getMapper(), spotId,
					type.toString(), activity.toString());
		}

	}

	@Override
	public List<SpotRefrence> queryRefrencesByRefrenceIdAndSpotId(
			Long refrenceId, Long spotId, Activity activity, Page page) {
		// TODO Auto-generated method stub
		if (page == null) {
			List<SpotRefrence> list = jdbcTemplate.query(
					queryRefrencesByRefrenceIdAndSpotIdSQL,
					SpotRefrenceMapper.getMapper(), refrenceId, spotId,
					activity.toString());
			return list;

		} else {

			return this.getPaginationResult(
					queryRefrencesByRefrenceIdAndSpotIdSQL, page,
					SpotRefrenceMapper.getMapper(), refrenceId, spotId,
					activity.toString());
		}
	}

	@Override
	public List<SpotRefrence> queryRefrencesByType(SpotRefrenceType type,
			Activity activity, Page page) {
		// TODO Auto-generated method stub
		if (page == null) {
			List<SpotRefrence> list = jdbcTemplate.query(
					queryRefrencesByTypeSQL, SpotRefrenceMapper.getMapper(),
					type, activity.toString());
			return list;
		} else {
			return this.getPaginationResult(queryRefrencesByTypeSQL, page,
					SpotRefrenceMapper.getMapper(), type, activity.toString());
		}
	}

	@Override
	public List<SpotRefrence> queryRefrencesBySpotNameAndType(String spotName,
			SpotRefrenceType type, Activity activity, Page page) {
		// TODO Auto-generated method stub

		spotName = "?" + spotName + "?";
		List<SpotRefrence> list = jdbcTemplate.query(
				queryRefrencesBySpotNameAndTypeSQL,
				SpotRefrenceMapper.getMapper(), spotName, type,
				activity.toString());
		return list;
	}

	@Override
	public List<SpotRefrence> queryRefrencesByProviderIdAndSpotIdAndType(
			Long providerId, Long spotId, SpotRefrenceType type,
			Activity activity, Page page) {
		// TODO Auto-generated method stub
		if (page == null) {
			List<SpotRefrence> list = jdbcTemplate.query(
					queryRefrencesByProviderIdAndSpotIdAndTypeSQL,
					SpotRefrenceMapper.getMapper(), providerId, spotId,
					type.toString(), activity.toString());
			return list;
		} else {
			return this.getPaginationResult(
					queryRefrencesByProviderIdAndSpotIdAndTypeSQL, page,
					SpotRefrenceMapper.getMapper(), providerId, spotId,
					type.toString(), activity.toString());
		}
	}

	@Override
	public List<SpotRefrence> queryRefrencesByProviderIdAndSpotId(
			Long providerId, Long spotId, Activity activity, Page page) {
		// TODO Auto-generated method stub
		if (page == null) {
			List<SpotRefrence> list = jdbcTemplate.query(
					queryRefrencesByProviderIdAndSpotIdSQL,
					SpotRefrenceMapper.getMapper(), providerId, spotId,
					activity.toString());
			return list;
		} else {
			return this.getPaginationResult(
					queryRefrencesByProviderIdAndSpotIdSQL, page,
					SpotRefrenceMapper.getMapper(), providerId, spotId,
					activity.toString());
		}
	}

	@Override
	public SpotRefrence queryRefrenceById(Long id, Activity activity) {
		// TODO Auto-generated method stub
		List<SpotRefrence> list = jdbcTemplate.query(queryRefrenceByIdSQL,
				SpotRefrenceMapper.getMapper(), id, activity.toString());
		return list.get(0);
	}

	@Override
	public Integer getRefrenceCountBySpotIdAndType(Long spotId,
			SpotRefrenceType type, Activity activity) {
		// TODO Auto-generated method stub
		Integer count = jdbcTemplate.queryForInt(
				getRefrenceCountBySpotIdAndTypeSQL,
				SpotRefrenceMapper.getMapper(), spotId, type.toString(),
				activity.toString());
		return count;
	}

	@Override
	public Integer getRefrenceCountBySpotIdAndProviderIdAndType(Long spotId,
			Long providerId, SpotRefrenceType type, Activity activity) {
		// TODO Auto-generated method stub
		Integer count = jdbcTemplate.queryForInt(
				getRefrenceCountBySpotIdAndProviderIdAndTypeSQL,
				SpotRefrenceMapper.getMapper(), spotId, providerId,
				type.toString(), activity.toString());
		return count;
	}

	@Override
	public Integer getRefrenceCountBySpotIdAndProviderId(Long spotId,
			Long providerId, Activity activity) {
		// TODO Auto-generated method stub
		Integer count = jdbcTemplate.queryForInt(
				getRefrenceCountBySpotIdAndProviderIdSQL,
				SpotRefrenceMapper.getMapper(), spotId, activity.toString());
		return count;
	}

	/******************************* delete ***************************/
	@Override
	public void deleteRefrenceById(Long id) {
		// TODO Auto-generated method stub
		jdbcTemplate.update(deleteRefrenceByIdSQL, id);
	}

	@Override
	public void deleteRefrenceByRefrenceIdAndSpotIdAndType(Long refrenceId,
			Long spotId, SpotRefrenceType type) {
		// TODO Auto-generated method stub
		jdbcTemplate.update(deleteRefrenceByRefrenceIdAndSpotIdAndTypeSQL,
				refrenceId, spotId, type.toString());
	}

	@Override
	public void deleteRefrenceByRefrenceIdAndSpotId(Long refrenceId, Long spotId) {
		// TODO Auto-generated method stub
		jdbcTemplate.update(deleteRefrenceByRefrenceIdAndSpotIdSQL, refrenceId,
				spotId);
	}

	@Override
	public void deleteRefrenceByProviderIdAndSpotId(Long providerId, Long spotId) {
		// TODO Auto-generated method stub
		jdbcTemplate.update(deleteRefrenceByProviderIdAndSpotIdSQL, providerId,
				spotId);
	}

	@Override
	public void deleteRefrenceByProviderIdAndSpotIdAndType(Long providerId,
			Long spotId, SpotRefrenceType type) {
		// TODO Auto-generated method stub

		jdbcTemplate.update(deleteRefrenceByProviderIdAndSpotIdAndTypeSQL,
				providerId, spotId, type.toString());

	}

}
